我有一个像这样的散列h={1=>{"inner"=>45},2=>{"inner"=>46},"inner"=>47}如何删除包含键“inner”的每一对?你可以看到一些“内部”对直接出现在h中,而另一些成对出现在h中请注意,我只想删除“内部”对,因此如果我对上述哈希调用我的批量删除方法,我应该得到h={1=>{},2=>{}}因为这些对没有键=="inner" 最佳答案 真的,这就是拒绝!用于:deff!xx.reject!{|k,v|'inner'==k}ifx.is_a?Hashx.each{|k,v|f!x[k]}end
proc=Proc.newdo|name|puts"Thankyou#{name}!"enddefthankyieldendproc.call#outputnothing,justfineproc.call('God')#=>ThankyouGod!thank&proc#outputnothing,too.Fine;thank&proc('God')#Error!thank&proc.call('God')#Error!thankproc.call('God')#Error!#So,whatshouldIdoifIhavetopassthe'God'totheprocandusethe
这是我现在所拥有的,它有点管用:defpadding(a,b,c=nil)untila[b-1]a这是它起作用的时候:a=[1,2,3]padding(a,10,"YES")=>[1,2,3,"YES","YES","YES","YES","YES","YES","YES"]a[1,2,3]padding(a,10,1)=>[1,2,3,1,1,1,1,1,1,1]但是当我没有为“c”输入值时它崩溃了a=[1,2,3]padding(a,10)Killed我应该如何附加它以避免崩溃?此外,您建议如何更改此方法以按如下方式使用它:[1,2,3].padding(10)=>[1,2,3,n